import { defineStore } from 'pinia'

const useTodosStore = defineStore('todos', {
    state: () => ({
        list: [
            {
                id: 1,
                name: '吃饭',
                done: false,
            },
            {
                id: 2,
                name: '睡觉',
                done: true,
            },
            {
                id: 3,
                name: '打豆豆',
                done: false,
            },
        ],
    }),
    
    actions: {
        checkAll(value) {
            this.list.forEach((item) => (item.done = value))
        },

        delTodo(id) {
            this.list = this.list.filter((item) => item.id !== id)
        },

        changeDone(id) {
            const todo = this.list.find((item) => item.id === id)
            todo.done = !todo.done
        },
    },
    getters: {
        // 根据所有的单选按钮，先确定全选按钮的状态
        isCheckAll() {
            return this.list.every((item) => item.done)
        },
    },

})

const actions = {
    changeDone(id) {
        const todo = this.list.find((item) => item.id === id)
        todo.done = !todo.done
    },
}


export default useTodosStore